﻿using System;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraNavBar;
using xfsale.lib;

namespace xfsale.ui
{
    public partial class WinMain : XtraForm
    {
        public WinMain()
        {
            InitializeComponent();

            foreach (DevExpress.Skins.SkinContainer cnt in DevExpress.Skins.SkinManager.Default.Skins)
            {
                BarButtonItem skin = new BarButtonItem(BarManager, cnt.SkinName);
                MnuStyle.AddItem(skin);
                skin.ItemClick += new ItemClickEventHandler(MnuStyle_ItemClick);
            }

            int i = 0;

            NavBarControl bar = null;
            NavBarGroup group = null;
            NavBarItem item = null;
            BarSubItem mnu = null;
            BarSubItem mnu2 = null;
            BarButtonItem mnu3 = null;
            foreach (SysMenu menu in App.MenuSet.Values)
            {
                //初始化一级菜单
                if (menu.Id.Length == 2)
                {
                    //新建MainBar
                    BarLargeButtonItem btn = new BarLargeButtonItem();
                    btn.Border = BorderStyles.HotFlat;
                    btn.ButtonStyle = BarButtonStyle.Check;
                    btn.Name = menu.Id;
                    if (!String.IsNullOrEmpty(menu.Icon))
                    {
                        btn.LargeGlyph = System.Drawing.Image.FromFile("Image/" + menu.Icon);
                    }
                    btn.ItemClick += new ItemClickEventHandler(BarBtn_ItemClick);
                    btn.Down = (i == 0);
                    btn.Tag = menu;
                    BarMain.LinksPersistInfo.Add(new LinkPersistInfo(btn, true));

                    mnu = new BarSubItem();
                    mnu.Name = menu.Id;
                    mnu.Caption = menu.CaptionFull;
                    mnu.Tag = menu;
                    BarMenu.AddItem(mnu);

                    //新建NavBar
                    bar = new NavBarControl();
                    bar.Name = menu.Id;
                    bar.Text = menu.Caption;
                    bar.Dock = DockStyle.Fill;
                    bar.Visible = (i == 0);
                    bar.LargeImages = ImgBar;
                    bar.SmallImages = ImgBar2;
                    bar.Tag = menu;
                    Splitter.Panel1.Controls.Add(bar);

                    i++;
                }

                //初始化二级菜单
                if (menu.Id.Length == 4)
                {
                    //新建NavBarGroup
                    group = new NavBarGroup();
                    group.Name = menu.Id;
                    group.Caption = menu.Caption;
                    group.GroupStyle = NavBarGroupStyle.SmallIconsList;
                    group.Expanded = true;
                    group.LargeImageIndex = 0;
                    group.SmallImageIndex = -1;
                    bar.Groups.Add(group);

                    mnu2 = new BarSubItem();
                    mnu2.Name = menu.Id;
                    mnu2.Caption = menu.CaptionFull;
                    mnu2.Tag = menu;
                    mnu.AddItem(mnu2);
                }

                //初始化三级菜单
                if (menu.Id.Length == 6)
                {
                    //新建NavBarGroup
                    item = new NavBarItem();
                    item.Name = menu.Id;
                    item.Caption = menu.Caption;
                    item.LargeImageIndex = -1;
                    item.SmallImageIndex = 0;
                    item.Tag = menu;
                    item.LinkClicked += new NavBarLinkEventHandler(NavItem_LinkClicked);
                    group.ItemLinks.Add(item);

                    mnu3 = new BarButtonItem();
                    mnu3.Name = menu.Id;
                    mnu3.Caption = menu.CaptionFull;
                    mnu3.Tag = menu;
                    mnu3.ItemClick += new ItemClickEventHandler(MenuItem_ItemClick);
                    mnu2.AddItem(mnu3);

                    if (string.IsNullOrEmpty(menu.UIFrame))
                    {
                        item.Enabled = false;
                    }
                }
            }

            //LblRegionCode.Caption = App.Op.RegionCode;
            LblBranchCode.Caption = App.Op.BranchCode;
            LblOpID.Caption = App.Op.OpCode + " " + App.Op.OpName;
            LblSysStatus.Caption = App.Sys.StatusText;
            LblSysWorkDay.Caption = App.Sys.WorkDay;
            LblSysVersion.Caption = App.Sys.Version;
            LblTitle.Text = string.Empty;
        }

        void MnuStyle_ItemClick(object sender, ItemClickEventArgs e)
        {
            string skinName = e.Item.Caption;
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(skinName);
            BarManager.GetController().PaintStyleName = "Skin";
        }

        void BarBtn_ItemClick(object sender, ItemClickEventArgs e)
        {
            BarLargeButtonItem item = e.Item as BarLargeButtonItem;
            SysMenu menu = item.Tag as SysMenu;
            foreach (BarItemLink i in BarMain.ItemLinks)
            {
                (i.Item as BarLargeButtonItem).Down = (i.Item == item);
            }
            foreach (Control i in Splitter.Panel1.Controls)
            {
                i.Visible = (item.Tag == i.Tag);
            }
        }

        void MenuItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            SysMenu menu = (e.Item as BarButtonItem).Tag as SysMenu;
            ClickMenuItem(menu);
        }

        void NavItem_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            SysMenu menu = (sender as NavBarItem).Tag as SysMenu;
            ClickMenuItem(menu);
        }

        void ClickMenuItem(SysMenu menu)
        {
            if (string.IsNullOrEmpty(menu.UIFrame))
                return;

            try
            {
                //Splitter.Panel1.Enabled = false;
                if (PnlMain.Controls.Count > 0)
                    return;

                SysMenu menu1 = App.MenuSet[menu.Id.Substring(0, 2)];
                SysMenu menu2 = App.MenuSet[menu.Id.Substring(0, 4)];

                foreach (BarItemLink i in BarMain.ItemLinks)
                {
                    (i.Item as BarLargeButtonItem).Down = (i.Item.Tag == menu1);
                }
                foreach (Control i in Splitter.Panel1.Controls)
                {
                    i.Visible = (menu1 == i.Tag);
                }

                /*foreach (Control ctrl in Splitter.Panel2.Controls)
                {
                    ctrl.Dispose();
                }
                Splitter.Panel2.Controls.Clear();*/
                /*NavBarControl NavBar = (Splitter.Panel1.Controls[0] as NavBarControl);
                foreach(NavBarGroup group in NavBar.Groups)
                {
                    foreach(NavBarItemLink item in group.VisibleItemLinks)
                    {
                        item.Item.Enabled = false;
                    }
                }*/

                string dll = null;
                if (!string.IsNullOrEmpty(menu.UIDll))
                    dll = menu.UIDll;
                FrmBase frame = Activator.CreateInstance(dll, menu.UIFrame).Unwrap() as FrmBase;
                frame.Name = menu.Id;
                frame.Tag = menu;
                frame.Dock = DockStyle.Left;
                PnlMain.Controls.Add(frame);

                LblTitle.Text = App.MenuSet[menu.Id.Substring(0, 2)].Caption + " → "
                    + App.MenuSet[menu.Id.Substring(0, 4)].Caption + " → "
                    + menu.Caption;

                //NavPanel.Enabled = false;
            }
            catch (Exception ex)
            {
                SysMessage.Error("载入窗体失败. " + ex.Message);
            }
        }

        void PnlMain_ControlRemoved(object sender, ControlEventArgs e)
        {
            LblTitle.Text = string.Empty;
        }

        private void BtnLogout_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Close();
        }

        private void WinMain_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                //Splitter.Panel2.ContentImageAlignement = System.Drawing.ContentAlignment.TopLeft;
                //Splitter.Panel2.ContentImage = System.Drawing.Image.FromFile("Image/背景.gif");

                //注册快捷键
                bool success = RegisterHotKey(Handle, 100, KeyModifiers.Alt, Keys.Oemtilde);
                if (!success)
                {
                    SysMessage.Error("注册快捷键出错");
                }
            }
        }

        private void WinMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!this.DesignMode)
            {
                //取消快捷键
                bool success = UnregisterHotKey(Handle, 100);
                if (!success)
                {
                    SysMessage.Error("取消快捷键出错");
                }
            }
        }

        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;

            switch (m.Msg)
            {
                case WM_HOTKEY:
                    if (ActiveForm == this)
                    {
                        WinKey winKey = new WinKey();
                        winKey.ShowDialog(this);
                    }
                    break;
            }
            base.WndProc(ref m);
        }

        [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(
            IntPtr hWnd,                // handle to window    
            int id,                     // hot key identifier    
            KeyModifiers fsModifiers,   // key-modifier options    
            Keys vk                     // virtual-key code    
        );

        [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(
            IntPtr hWnd,                // handle to window    
            int id                      // hot key identifier    
        );

        [Flags()]
        public enum KeyModifiers
        {
            None = 0,
            Alt = 1,
            Control = 2,
            Shift = 4,
            Windows = 8
        }
    }
}
